<template>
  <div>
    <div>
      <el-form :inline="true" @submit.native.prevent>
        <el-form-item>
          <el-input v-model="search" placeholder="请输入标题" @keyup.enter.native="handleSearch" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-table v-loading="table.loading" :data="table.data" border>
        <el-table-column prop="title" label="标题" />
        <el-table-column label="类型" width="70">
          <template slot-scope="t">
            <el-tag v-if="t.row.type === 1" type="warning">动态</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="状态" width="80">
          <template slot-scope="t">
            <el-tag v-if="t.row.status === 2" type="success">已发布</el-tag>
            <el-tag v-if="t.row.status === 1" type="warning">草稿</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="创建时间" width="160" />
        <el-table-column label="操作" width="200">
          <template slot-scope="t">
            <el-button v-if="t.row.status === 1" type="primary" size="mini" @click="handleStatus(t.row.id,2)">发布</el-button>
            <el-button v-if="t.row.status === 2" type="danger" size="mini" @click="handleStatus(t.row.id,1)">撤回</el-button>
            <el-button v-if="t.row.status === 1" type="primary" size="mini" @click="handleEdit(t.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <WrapPagination>
      <el-pagination
        class="pagination"
        :current-page="table.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="table.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="table.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </WrapPagination>
  </div>
</template>

<script>
import { TableMixin } from '../../components/mixin'
import { getList, updateStatus } from '../../api/moment'

export default {
  mixins: [TableMixin],
  data() {
    return {
      search: ''
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      this.wrapResourceList(getList, {
        search: this.search,
        type: 1
      })
    },
    handleSearch() {
      this.table.page = 1
      this.getList()
    },
    handleEdit(row) {
      this.$router.push('/moment/' + row.id)
    },
    handleStatus(id, status) {
      const message = {
        2: '您正在发布动态, 是否继续?',
        1: '您正在撤销该动态的发布, 是否继续?'
      }
      this.$confirm(message[status], '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        customClass: 'confirmDialog',
        center: true,
        type: 'warning'
      }).then(() => {
        updateStatus(id, status).then(() => {
          this.$message.success('操作成功')
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    }
  }
}
</script>
